<?php

namespace App\Http\Controllers\Api\V1;

use Carbon\Carbon;
use App\Models\Product;
use App\Models\Category;
use App\Http\Controllers\Api\V1\Resources\ProductResource;
use App\Services\Cache\ProductCache;

class ProductsController extends Controller
{
	public function index()
	{
		$products = app(ProductCache::class)->products();
		return ProductResource::collection($products);
	}

	

	public function store()
	{
		$user = request()->user();
		$brandId = request('brand_id');
		if(empty($brandId)){

			fail('请选择品牌！');
		}

		$brandModelId = request('brand_model_id');
		if(empty($brandModelId)){

			fail('请选择车型！');
		}


		$price = request('price');
		if(!$price){

			fail('请填写收车价！');
		}

		$tradePrice = request('trade_price');
		if(!$tradePrice){

			fail('请填写同行价！');
		}

		$lsPrice = request('ls_price');
		if(!$lsPrice){

			fail('请填写零售价！');
		}

		$registerTime = request('register_time');
		if(!$registerTime){

			fail('请填写上牌时间！');
		}

		$mileage = request('mileage');
		if(!$mileage){

			fail('请填写行驶里程！');
		}

		$times = request('times');

		$standardId = request('standard_id');
		if(!$standardId){

			fail('请选择环保标准！');
		}
		$displacement = request('displacement');
		if(!$displacement){

			fail('请填写排量！');
		}

		$typeId = request('type_id');
		if(!$typeId){

			fail('请选择类型！');
		}

		$description = request('description');
		if(!$description){

			$description="无瑕疵";
		}

		$title = request('title');
		if(!$title){

			fail('请填写标题！');
		}
		$provinceCode = request('province_code');
		$province = request('province');
		if(!$provinceCode){

			fail('请选择所在省！');
		}

		$cityCode = request('city_code');
		$city = request('city');
		if(!$cityCode){

			fail('请选择所在市！');
		}
		$isHidden = request('is_hidden');
		$isOpen = request('is_open');
		$isSale = request('is_sale');
		$image = request('image');
		
		if(!$image){

			fail('请上传图片！');
		}else{
			$image = json_encode($image);
		}

		$xcImage = request('xc_image');
		
		/*if(!$xcImage){

			fail('请上传瑕疵图片！');
		}*/
		/*$zqImage = request('zq_image');
		
		if(!$zqImage){

			fail('请上传左前图片！');
		}
		$yhImage = request('yh_image');
		
		if(!$yhImage){

			fail('请上传右后图片！');
		}
		$xjImage = request('xj_image');
		
		if(!$xjImage){

			fail('请上传细节图片！');
		}*/

		$age = Carbon::parse($registerTime);
 
		// 获取当前日期
		$now = Carbon::now();
		 
		// 计算差值，并四舍五入到最接近的整数
		$roundedAge = $age->diffInYears($now);

		// if(empty($xcImage)){
		// 	$description = "无瑕疵";
		// }

		$params = [
			'brand_id' => $brandId,
			'price' => $price,
			'register_time' => $registerTime,
			'mileage'=>$mileage,
			'times' => $times,
			'standard_id' => $standardId,
			'displacement' => $displacement,
			'type_id' => $typeId,
			'description' => $description,
			'title'=>$title,
			'is_hidden' => $isHidden,
			'is_open' => $isOpen,
			'is_sale' => $isSale,
			'image' => $image,
			'xc_image' => $xcImage,
			/*'zq_image' => $zqImage,
			'yh_image' => $yhImage,
			'xj_image' => $xjImage,*/
			'age'=>$roundedAge,
			'province_code'=>$provinceCode,
			'province' => $province,
			'city_code'=>$cityCode,
			'city' => $city,
			'trade_price' => $tradePrice,
			'ls_price' => $lsPrice,
			'brand_model_id'=>$brandModelId,
			'latitude'=>$user->latitude,
			'longitude'=>$user->longitude,
		];

		

		$user->products()->create($params);


		return response()->json(['code' => 200]);
		/*$keyword = request('keyword');
		$products =  app(ProductCache::class)->search($keyword);
		return ProductResource::collection($products);*/
	}



	public function edit()
	{
		$user = request()->user();
		$productId = request('product_id');
		if(empty($productId)){

			fail('当前产品不存在！');
		}

		$brandId = request('brand_id');
		if(empty($brandId)){

			fail('请选择品牌！');
		}

		$brandModelId = request('brand_model_id');
		if(empty($brandModelId)){

			fail('请选择车型！');
		}


		$price = request('price');
		if(!$price){

			fail('请填写收车价！');
		}

		$tradePrice = request('trade_price');
		if(!$tradePrice){

			fail('请填写同行价！');
		}

		$lsPrice = request('ls_price');
		if(!$lsPrice){

			fail('请填写零售价！');
		}


		$registerTime = request('register_time');
		if(!$registerTime){

			fail('请填写上牌时间！');
		}

		$mileage = request('mileage');
		if(!$mileage){

			fail('请填写行驶里程！');
		}

		$times = request('times');

		$standardId = request('standard_id');
		if(!$standardId){

			fail('请选择环保标准！');
		}
		$displacement = request('displacement');
		if(!$displacement){

			fail('请填写排量！');
		}

		$typeId = request('type_id');
		if(!$typeId){

			fail('请选择类型！');
		}

		$description = request('description');
		if(!$description){

			$description="无瑕疵";
		}

		$title = request('title');
		if(!$title){

			fail('请填写标题！');
		}
		$provinceCode = request('province_code');
		$province = request('province');
		if(!$provinceCode){

			fail('请选择所在省！');
		}

		$cityCode = request('city_code');
		$city = request('city');
		if(!$cityCode){

			fail('请选择所在市！');
		}
		$isHidden = request('is_hidden');
		$isOpen = request('is_open');
		$isSale = request('is_sale');
		$image = request('image');
		
		if(!$image){

			fail('请上传图片！');
		}else{
			$image = json_encode($image);
		}

		$xcImage = request('xc_image');

		// if(empty($xcImage)){
		// 	$description = "无瑕疵";
		// }
		
		/*if(!$xcImage){

			fail('请上传瑕疵图片！');
		}*/
		/*$zqImage = request('zq_image');
		
		if(!$zqImage){

			fail('请上传左前图片！');
		}
		$yhImage = request('yh_image');
		
		if(!$yhImage){

			fail('请上传右后图片！');
		}
		$xjImage = request('xj_image');
		
		if(!$xjImage){

			fail('请上传细节图片！');
		}*/

		$age = Carbon::parse($registerTime);
 
		// 获取当前日期
		$now = Carbon::now();
		 
		// 计算差值，并四舍五入到最接近的整数
		$roundedAge = $age->diffInYears($now);


		$params = [
			'brand_id' => $brandId,
			'price' => $price,
			'register_time' => $registerTime,
			'mileage'=>$mileage,
			'times' => $times,
			'standard_id' => $standardId,
			'displacement' => $displacement,
			'type_id' => $typeId,
			'description' => $description,
			'title'=>$title,
			'is_hidden' => $isHidden,
			'is_open' => $isOpen,
			'is_sale' => $isSale,
			'image' => $image,
			'xc_image' => $xcImage,
			/*'zq_image' => $zqImage,
			'yh_image' => $yhImage,
			'xj_image' => $xjImage,*/
			'age'=>$roundedAge,
			'province_code'=>$provinceCode,
			'province' => $province,
			'city_code'=>$cityCode,
			'city' => $city,
			'trade_price' => $tradePrice,
			'ls_price' => $lsPrice,
			'brand_model_id'=>$brandModelId,
			'finial_price'=>request('finial_price'),
			'latitude'=>$user->latitude,
			'longitude'=>$user->longitude,
		];

		

		Product::find($productId)->update($params);


		return response()->json(['code' => 200]);
		/*$keyword = request('keyword');
		$products =  app(ProductCache::class)->search($keyword);
		return ProductResource::collection($products);*/
	}
	
	public function search()
	{
		$keyword = request('keyword');
		$products =  app(ProductCache::class)->search($keyword);
		return ProductResource::collection($products);
	}
	
	public function show($id)
	{
		$product = app(ProductCache::class)->product($id);
		return new ProductResource($product);
	}
}

